import subprocess
import time
from functools import partial #用来固定某个参数的固定值
subprocess.Popen=partial(subprocess.Popen,encoding='utf-8')
import execjs
import json
import ddddocr
import requests
def cap_union_new_getsig():
    url = "https://captcha.yunpian.com/v1/jsonp/captcha/get"
    params = {
        'cb':'lg2jgl3ok8',
        'i':js_code['i'],
        'k': js_code['k'],
        'captchaId':js_code['captchaId']
    }
    response = sess.get(url, headers=headers, params=params)
    data =  json.loads(response.text.replace('ypjsonp(','').replace(')',''))
    with open('../static/bg.jpg','wb') as f:
        f.write(requests.get(url = data['data']['bg']).content)
    with open('../static/fg.png', 'wb') as f:
        f.write(requests.get(url = data['data']['front']).content)
    return data['data']['token']

def getSeild():
    ocr = ddddocr.DdddOcr(det=False,ocr=False,show_ad=False)
    with open('../static/bg.jpg','rb') as f:
        bg = f.read()
    with open('../static/fg.png', 'rb') as f:
        fg = f.read()
    target = ocr.slide_match(bg,fg,simple_target=True)
    return target

def verify():
    url = 'https://captcha.yunpian.com/v1/jsonp/captcha/verify'

    windth = getSeild()['target'][0] * 0.633333333333333
    target = (((480 * 0.633333333333333)  - (93 * 0.633333333333333)) * (windth / ((480 - 42) * 0.633333333333333))) / (480 * 0.633333333333333)
    print(target)

    js_codec = dict(execjs.compile(open('./云片滑块.js', 'r', encoding='utf-8').read()).call('verify',target))
    params = {
        'cb': 'lg2jgl3ok8',
        'i': js_codec['i'],
        'k': js_codec['k'],
        'token':cap_union_new_getsig(),
        'captchaId': js_code['captchaId']
    }

    time.sleep(3)
    response = sess.get(url, headers=headers, params=params)
    print(response.text)

if __name__ == '__main__':
    sess = requests.session()
    js_code = dict(execjs.compile(open('./云片滑块.js', 'r', encoding='utf-8').read()).call('encrypts'))
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.47",
    }
    for i in range(10):
        verify()